com.sxnsx.xnaja.downloader.abs_downloader = function () {
    var pub = {};
    var priv = {};
    var tag = "com.sxnsx.xnaja.downloader.abs_downloader";
    
    this.working = false;
    this.total_page = 0;
    this.total_photo = 0;
    this.page_from = 0;
    this.page_to = 0;
    this.save_path = "";
    this.method = "";
    priv.collection = "";


    /* 
     * public methods: prompt, stop, do 
     * */
    this.prompt = function (url, method) {
        var msg = '';
        for (var i in xnaja_app.utilities)
            msg += i.toString() + "\n";
        setTimeout(function() { throw new Error("[debug] " + msg); }, 0);
        var prompt_params = xnaja_app.utilities.download_prompt(url, method, -1, -1, []);
        if (prompt_params == null) {
            return false;
        } else {
            this.page_from = prompt_params.page_from;
            this.page_to = prompt_params.page_to;
            this.save_path = prompt_params.save_path;
            this.method = prompt_params.download_method;
        }
        return true;
    };

    this.stop = function () {this.working = false;};

    this.do = function(url) {
        var p = xnaja_app.utilities.get_os().search("WINNT")==-1?"/":"\\";
        var _this = this;
        function merge_link(link) {
            priv.collection += '<a href="' + link + '" />' + link+  '<br/>';
        };

        function download(link) {
            var file_name = link.substr(link.lastIndexOf('\/') + 1);
            xnaja_app.utilities.save_to_file(link,  _this.save_path + p + file_name);
        };
        if (this.method == 'get_links') {
            priv.collection = '';
            var args_box = document.getElementById('args_box');
            this.do_proc(url, merge_link);
            args_box.value = priv.collection;
            window.open('chrome://xnaja/content/links.html');
        } else {
            this.do_proc(url, download);
        }

    };
};
com.sxnsx.cexport(com.sxnsx.xnaja.downloader.abs_downloader);


